//
//  NumberKeyboardTF.m
//  ZITHER
//
//  Created by weixin on 2018/8/2.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "NumberKeyboardTF.h"
#import "NumberKeyboard.h"
@implementation NumberKeyboardTF



-(void)awakeFromNib{
    [super awakeFromNib];
    
    NumberKeyboard *keyboard = [[NumberKeyboard alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 240)];
    [keyboard setBtnBlock:^(NSInteger index) {
        
                    if (index !=10) {
                        
                        UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGenerator alloc]initWithStyle:2];
        
                        [impactLight impactOccurred];
        
                        if (index==12) {
        
                            if (self.text.length!=0) {
        
                                NSString * str = self.text;
        
                                self.text = [str substringToIndex:str.length-1];
        
                            }
        
                        }else{
                            if (index == 11) {
                                  self.text = [NSString stringWithFormat:@"%@0",self.text];
                            }else{
                                  self.text = [NSString stringWithFormat:@"%@%ld",self.text,index];
                            }
        
                        }
                    }
    }];
    
    self.inputView = keyboard;
    
    
    self.font = [UIFont systemFontOfSize:25];
    self.placeholderColor = [UIColor whiteColor]; // 自定义 placeholder 的颜色
    self.returnKeyType = UIReturnKeyDone;
    self.enablesReturnKeyAutomatically = YES;
    self.tintColor = [UIColor whiteColor];
    self.typingAttributes = @{NSFontAttributeName: UIFontMake(35),
                              NSForegroundColorAttributeName: [UIColor whiteColor],
                              NSParagraphStyleAttributeName: [NSMutableParagraphStyle qmui_paragraphStyleWithLineHeight:40]};
    // 限制可输入的字符长度
    self.maximumTextLength = 11;
    
    self.qmui_borderPosition = QMUIViewBorderPositionBottom;
    self.qmui_borderWidth = 2;
    self.qmui_borderColor = [UIColor whiteColor];
//    self.keyboardAppearance = UIKeyboardAppearanceDark;


}



-(bool)Mobile:(NSString*)mobile
{
    
    /**
     * 移动号段正则表达式
     */
    NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    /**
     * 联通号段正则表达式
     */
    NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    /**
     * 电信号段正则表达式
     */
    NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
    BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
    BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
    NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
    BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
    
    if (isMatch1 || isMatch2 || isMatch3) {
        return YES;
    }else{
        return NO;
    }
    
}

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = self.font.lineHeight + 10;
    originalRect.size.width = 4;
    return originalRect;
}


@end
